package com.sky.service.admin.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
   @Autowired
   CategoryMapper categoryMapper;
   @Autowired
   DishMapper dishMapper;
   @Autowired
   SetmealMapper setmealMapper;
   @Autowired
   private RedisTemplate redisTemplate;

    @Override
    public void AddCuisine(CategoryDTO categoryDTO) {

        //1.创建Category对象
        Category category = new Category();
        //2.将dto的属性值拷贝到Category对象中
        BeanUtils.copyProperties(categoryDTO,category);
        //3. category属性其他初始化
           //设置时间和操作人
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        //分类状态默认值为0
        category.setStatus(StatusConstant.ENABLE);


        //插入数据到数据库
         categoryMapper. AddCuisine(category);






    }

    @Override
    public List<Category> list(Integer type) {
      //1.先从redis获取数据
      List<Category> redisList = (List<Category>) redisTemplate.opsForValue().get("category"+type);  //创建redis的string类型对象
       //2.如果没有拿到，则从数据库中拿数据，然后存进redis中
        if (redisList == null || redisList .size()== 0){  //当redis中为null或者redis的长度为0时，则说明从redis中没有拿到数据
           log.info("从数据库中获取数据");
            redisList = categoryMapper.list(type);  //从数据库中拿数据
            redisTemplate.opsForValue().set("category"+type,redisList);  //将从数据库中获取的数据存进redis

        }else {
            log.info("从redis中获取数据");


        }
        return  redisList;
    }

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
         //分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());


        //底层数组进行分页
        Page<Category> p =categoryMapper.page(categoryPageQueryDTO);

        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public Result<String> operate(Long id, Integer status) {

        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();

        return categoryMapper.operate(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {

        Category category = new Category();

        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        BeanUtils.copyProperties(categoryDTO,category);

        categoryMapper.update(category);
    }

    @Override
    public void DeleteId(Long id) {

        //查询当前分类是否关联了菜品
        Integer count = dishMapper.countByCategoryId(id);
        if (count >0){
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐
        count = setmealMapper.countByCategoryId(id);
        if (count >0){
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

        }
        //删除分类数据
        categoryMapper.DeleteId();
    }
}
